package com.shoppingmall.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shoppingmall.bean.UserInfoBean;
import com.shoppingmall.service.RegisterService;

/**
 * 用户注册处理器
 * @author LIU
 * @version 1.0 2017-11-10
 */
@Controller
public class RegisterController {

	@Autowired
	private RegisterService registerService;
	
	
	/**
	 * 跳转注册界面
	 * @return String
	 */
	@RequestMapping("registerView")
	public String registerView() {
		System.out.println("跳转register.jsp页面");
		return "register";
	}
	
	
	
	
	/**
	 * 用户注册功能
	 * @param userInfo
	 * @param password
	 * @param response
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("register")
	@ResponseBody
	public void register(UserInfoBean userInfo,String password, HttpServletResponse response) throws Exception {
		
		System.out.println(userInfo + " " + password);
		
		boolean result = registerService.addUser(userInfo, password);
		response.setContentType("text/html;charset=utf-8");
		if(result) {
			response.getWriter().print("<script type='text/javascript'>alert('注册成功...');window.location='loginView';</script>");
		}
		
		response.getWriter().print("<script type='text/javascript'>alert('系统繁忙，请重试...');window.location='registerView';</script>");
	}

}
